﻿using ClientAppraise.Controls;
using ClientAppraise.Views.Pages.Corpus.CorpusType.Admin;
using ClientDao.Corpus;
using ClientInfrastructure.Model;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using ShareCode.Dto;
using ShareCode.Entity.Corpus;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Wpf.Ui.Controls;

namespace ClientAppraise.ViewModels.Pages.Corpus.CorpusType.Admin
{
    public partial class IndexViewModel(CorpusTypeDal typeDal) : ObservableObject
    {
        CorpusTypeDal Dal = typeDal;

        [ObservableProperty]
        private BaseTreeDataGridM<CorpusTypeEntity>? selectedItems;
        partial void OnSelectedItemsChanged(BaseTreeDataGridM<CorpusTypeEntity>? value)
        {
            IsEnabled = value != null;
        }

        [ObservableProperty]
        private bool isEnabled = false;


        [ObservableProperty]
        private ObservableCollection<BaseTreeDataGridM<CorpusTypeEntity>>? viewData;

        [RelayCommand]
        public void Add()
        {
            App.MyNavigation.NavigateWithHierarchy(typeof(NewPage));
        }

        [RelayCommand]
        public void AddSon()
        {
            App.MyNavigation.NavigateWithHierarchy(typeof(NewPage), new ClientInfrastructure.Dto.PageParameterDto() { Model = SelectedItems!.Entity, Do = ClientInfrastructure.Dto.PageParameterDtoDo.AddSon });
        }

        [RelayCommand]
        public void Edit()
        {
            App.MyNavigation.NavigateWithHierarchy(typeof(NewPage), new ClientInfrastructure.Dto.PageParameterDto() { Model = SelectedItems!.Entity, Do = ClientInfrastructure.Dto.PageParameterDtoDo.Update });
        }

        [RelayCommand]
        public async Task DeleteAsync()
        {
            if (SelectedItems != null && await new ZxMessageBox().ConfirmAsync("确定要删除选中项？") == Wpf.Ui.Controls.MessageBoxResult.Primary)
            {
                var reslut = await App.MyContentDialog.ShowRingLoadingAsync(async () =>
                   {
                       try
                       {
                           ResultDto<bool> result;

                           if (SelectedItems!.Entity!.IsDelete == 1)
                           {
                               result = await Dal.UnLogicDeleteAsync(SelectedItems.Entity.Id);
                           }
                           else
                           {
                               result = await Dal.LogicDeleteAsync(SelectedItems.Entity.Id);

                           }
                           if (result != null && result.Success && result.Data)
                           {
                               App.MySnackbar.Success("成功操作");
                           }
                           else
                           {
                               App.MySnackbar.Danger(result!.Message??"");
                           }
                       }
                       catch (Exception ex)
                       {
                           App.MySnackbar.Danger(ex.Message, ex.Message);
                       }
                       finally
                       {
                           App.MyContentDialog.CloseLoading();
                       }
                   });

                if (reslut == ContentDialogResult.Primary)
                {
                    await RefreshData();
                }

            }
        }

        public async Task RefreshData()
        {
            void fun(List<CorpusTypeEntity>? viewData)
            {
                if (viewData == null) return;

                if (!Application.Current.Dispatcher.CheckAccess())
                {
                    Application.Current.Dispatcher.Invoke(new Action(() => fun(viewData)));
                    return;
                }
                var _viewData = BaseTreeDataGridM<CorpusTypeEntity>.GetList(viewData);
                this.ViewData = _viewData;
            }

            await App.MyContentDialog.ShowRingLoadingAsync(async () =>
            {
                try
                {
                    var data = await Dal.ListAsync<CorpusTypeEntity>(new PagingDto { PageSize = 999999 });

                    if (data.Success)
                    {
                        fun(data.Data);
                    }
                }
                catch (Exception ex)
                {
                    App.MySnackbar.Danger(ex.Message, ex.Message);
                }
                finally
                {
                    App.MyContentDialog.CloseLoading();
                }
            });
        }

    }
}
